/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2014 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

//***********************************************************************************************************
//  Nuvoton Technology Corp.
//  Date: 23/Jan/2014
//  E-Mail: MicroC-8bit@nuvoton.com
//***********************************************************************************************************
//  Application: PWM Function
//  Set PWM output channel, PWM frequency and PWM duty before PWM running
//  PWM will be braked to a stop when P0.2 is low level
//
//  Output : PWM channel 0 => P0.1 (default)
//           PWM channel 1 => P1.6
//           PWM channel 2 => P1.7
//           PWM channel 3 => P0.0
//***********************************************************************************************************

//========================================= How to set PWM register =========================================
//
//  1.set load = 1; PWMRUN=1; ==> PWMCON0=0xC0;
//
//  2.Frequency == Fosc/(PWMP+1)
//    Use external OSC :Fosc = 11.0592MHz
//
//  3.Duty = PWMn/PWMP+1
//
//===========================================================================================================

//------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
//      <o0.0..9> PWM Period<0-1023>
//      <o1.0..9> PWM0 Duty<0-1023>
//      <o2.0..9> PWM1 Duty<0-1023>
//      <o3.0..9> PWM2 Duty<0-1023>
//      <o4.0..9> PWM3 Duty<0-1023>
// <h> UART pin Select
//     <o5.6> UART pin
//          <0=> Select P1.0, P1.1 as UART pin(default)
//          <1=> Select P2.6, P2.7 as UART pin(28 pin only)
// </h>
//-------------------------------- <<< end of configuration section >>> -------------------------------------

#define PWMP            0x3FF
#define PWM0_Duty       0x200
#define PWM1_Duty       0x100
#define PWM2_Duty       0x080
#define PWM3_Duty       0x3FF
#define Uart_Port_Sel   0x00
  


#include <stdio.h>
#include "N79E81x.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Version.h"
#include <intrins.h>

UINT16 PWM_shadow;
UINT16 PWM_old;
//-----------------------------------------------------------------------------------------------------------
void Init_PWM(void)
{

    PWM_shadow = PWMP;
    PWMPH = HIBYTE(PWM_shadow);         // PWM Period
    PWMPL = LOBYTE(PWM_shadow);
    PWMCON0 = 0xD0;                     // Enable PWM and select auto reload mode
}
void PWM_Pin_Recover_IO(void)
{
    PWMCON0 = 0x00;//
    PWMCON1 = 0x00;//
    PWMCON2 = 0x00;//
    PWMPH   = 0x00;//
    PWMPL   = 0x00;//
    PWM0H   = 0x00;//
    PWM0L   = 0x01;//
    PWM1H   = 0x00;//
    PWM1L   = 0x01;//
    PWM2H   = 0x00;//
    PWM2L   = 0x01;//
    PWM3H   = 0x00;//
    PWM3L   = 0x01;// PWMP < PWMn Duty, PWM Pin Output High(no-active)
    PWMCON0 = 0xD0;// PWM Run
    _nop_ ();      // NOP
    PWMCON0 = 0x2F;// PWM Stop
}
void PWM_Channel(E_PWMCNL_SEL Channel)
{
    switch(Channel)
    {
        case PWM_CHANNEL0:
            PWM_shadow=PWM0_Duty;
            PWM0H = HIBYTE(PWM_shadow);// PWM0 Duty (P0.1)
            PWM0L = LOBYTE(PWM_shadow);
            break;
        case PWM_CHANNEL1:
            PWM_shadow=PWM1_Duty;
            PWM1H = HIBYTE(PWM_shadow);// PWM1 Duty (P1.6)
            PWM1L = LOBYTE(PWM_shadow);
            break;
        case PWM_CHANNEL2:
            PWM_shadow=PWM2_Duty;
            PWM2H = HIBYTE(PWM_shadow);// PWM2 Duty (P1.7)
            PWM2L = LOBYTE(PWM_shadow);
            break;
        case PWM_CHANNEL3:
            PWM_shadow=PWM3_Duty;
            PWM3H = HIBYTE(PWM_shadow);// PWM3 Duty (P0.0)
            PWM3L = LOBYTE(PWM_shadow);
            break;
    }
}
void Enabled_Brake_Function(void)
{
    PWMCON1 = 0x30;                    // PWM is running. PWM will be stopped when P0.2 is low level.
}

void set_PWM_val(UINT16 PWM_VAL)
{
  if(PWM_old==PWM_VAL) return;//������������
	PWM_old=PWM_VAL;
	PWM3H = HIBYTE(PWM_old);// PWM3 Duty (P0.0)
    PWM3L = LOBYTE(PWM_old);
	Init_PWM(); 
	
}
//-----------------------------------------------------------------------------------------------------------
void init_pwm_all(void)
{
   
    PWM_Channel(PWM_CHANNEL3);           // Select PWM channel.
    Init_PWM();                        // Enable PWM function and set PWM period.
    Enabled_Brake_Function();          // Enable Brake function.
   
}
//-----------------------------------------------------------------------------------------------------------